package cn.dnui_zzh807.dnuifood_16110100807.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import cn.dnui_zzh807.dnuifood_16110100807.R;
import cn.dnui_zzh807.dnuifood_16110100807.controller.BaseActivity;

/**
 * Created by Dobby on 2018/11/5.
 */

public class Fragment2 extends Fragment {
    private BaseActivity baseActivity;
    private CollectFoodFragment collectFoodFragment;
    private CollectShopFragment collectShopFragment;
    private TextView collect_shop,collect_food;
    private View view = null;
    protected FragmentManager childManager;
    protected FragmentTransaction transaction;
    public Fragment2() {

    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        childManager = getChildFragmentManager();
        collect_shop =(TextView) view.findViewById(R.id.collect_shop);
        collect_food = (TextView) view.findViewById(R.id.collect_food);
        collect_shop.setSelected(true);
        collect_shop.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
        collect_shop.setTextSize(20);
        collect_food.setTextColor(getResources().getColor(R.color.colorPrimary));
        //initViews();
        initEvents();

    }
    @Override
    public void onResume() {

        super.onResume();
        if (collectShopFragment == null){
            collectShopFragment = new CollectShopFragment();
            addFragment(collectShopFragment);
        }else {
            replaceFragment(collectShopFragment);
        }
    }

    private void initEvents(){
        collect_shop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (collectShopFragment == null){
                    collectShopFragment = new CollectShopFragment();
                }
                replaceFragment(collectShopFragment);
                collect_shop.setSelected(true);
                collect_food.setSelected(false);
                collect_shop.setTextColor(getResources().getColor(R.color.colorAccent));
                collect_shop.setTextSize(20);
                collect_food.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
                collect_food.setTextSize(16);
            }
        });

        collect_food.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (collectFoodFragment == null){
                    collectFoodFragment = new CollectFoodFragment();
                }
                replaceFragment(collectFoodFragment);
                collect_shop.setSelected(false);
                collect_food.setSelected(true);
                collect_shop.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
                collect_shop.setTextSize(16);
                collect_food.setTextColor(getResources().getColor(R.color.colorAccent));
                collect_food.setTextSize(20);

            }
        });

    }

    protected void addFragment(Fragment fragment){

        transaction = childManager.beginTransaction();
        transaction.add(R.id.fragment_container,fragment);
        transaction.commit();

    }

    @Override
    public void onAttach(Context context) {

        super.onAttach(context);
    }

    protected void replaceFragment(Fragment fragment){
        transaction = childManager.beginTransaction();
        transaction.replace(R.id.fragment_container,fragment);
        transaction.commit();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater,container,savedInstanceState);
        View view=inflater.inflate(R.layout.fragment_fragment2, container, false);

        return view;
    }

}
